/**
 * @file Define the tracking service class
 * 
 * @author HengShan Lu
 */

const async = require('async');
const request = require('request');
const queryString = require('querystring');

const BaseService = require('./base-service').BaseService;
const { Configuration } = require('../constants');

/**
 * Wrap all methods about tracking
 * 
 * @class TrackingService
 * @extends {BaseService}
 */
class RetailService extends BaseService {

    /**
     * 
     * 获取会员列表信息
     * @param {*} params 
     * @param {*} parentCallback 
     */
    getGoodsList(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url = '';
        switch (params.type) {
            case 'goodsList':
            console.log('开始请求..')
                // VIP客户
                url = `${RetailServer}/wxserver/goods/selectGoodsListBysSalesclerk`;
                break;
            case '':
                postData = '';
        }
        delete params.type;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('parse数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: '查询出错'
                });
            }
        });
    }

    //////////// 删除商品
    DeleteGoods(params, parentCallback) {
        // console.log('开始请求..')
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url = `${RetailServer}/wxserver/goods/deleteGoodsBysSalesclerk`;
        
        delete params.type;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('parse数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: '查询出错'
                });
            }
        });
    }
    createNewContact(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var postData = {
            url: `${RetailServer}/dingdang/customer/editAndAddDeal`,
            method: 'POST',
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: '查询出错'
                });
            }
        });


    }

    getAcList(params, parentCallback) {
        // console.log('开始请求..')
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url = '';
        switch (params.type) {
            case 'acList':
                // VIP客户
                url = `${RetailServer}/wxserver/activity/getActivity`;
                break;
        }
        delete params.type;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('parse数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: '查询出错'
                });
            }
        });
    }
    
    /**
     * 
     * 获取最新来店客户
     * @param {*} params 
     * @param {*} parentCallback 
     */
    getVisitList(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;

        var { now, storeId } = params;
        var url = `${RetailServer}/dingdang/notice?cmd=getLatest&storeId=${storeId}&now=${now}`;
        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: '查询出错'
                });
            }
        });
    }

    /**
     * 
     * 保存用户基本信息
     * @param {*} params 
     * @param {*} parentCallback 
     */
    saveUserInfo(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url, param;
        url = `${RetailServer}/dingdang/customer/edit`;
        param = {
            customerId: params.customerId,
            customerName: params.customerName,
            nickname: params.nickname,
            gender: params.gender,
            email: params.email,
            mobile: params.mobile,
            address: params.address,
            contactId: params.contactId,
            tags: params.tags,
            isVip: params.isVip,
            isNative: params.isNative,
            contactId: params.contactId,
            styleName: params.styleName,
            colorName: params.colorName,
            sizeName: params.sizeName
        };

        var postData = {
            url: url,
            method: 'POST',
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify(param)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result == 1) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: '查询出错'
                });
            }
        });
    }

    /**
     * 更改用户头像
     */
    changeUserAvatar(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var postData = {
            url: `${RetailServer}/dingdang/user/saveHeadImage`,
            method: 'POST',
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify(params)
        };
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '异常，请稍后再试!'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        });

    }


    // 登录验证用户
    verifyUser(mobile, password, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;

        const url = `${RetailServer}/wxserver/user/login`;
        const param = {
            data: {
                mobile,
                password
            }
        };
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify(param)
        };
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '服务端异常，请稍后再试!'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        });

    }

    /**
     * Get the system user information by id
     * 
     * @name getUserProfile
     * @param {String} userId - The user id
     * @param {Function} parentCallback - The callback function
     * 
     * @returns {void}
     */
    getUserProfile(userId, parentCallback) {
        var me = this;
        // me.User.findUserById(userId, function (error, user) {
        //     parentCallback(user);
        // })
        parentCallback(null);
    }

    /**
     * 获取客户订单交易记录
     * @param {*} params 
     * @param {*} parentCallback 
     */
    getAssociatorOrder(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;

        var url = `${RetailServer}/dingdang/customer?cmd=listDeal&customerId=${params.customerId}&page=${params.page}&size=${params.size}`;
        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: '查询出错'
                });
            }
        })
    }

    // 获取顾客的到店、购买商品事件
    getAssociatorEvents(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;

        var url = `${RetailServer}/dingdang/timeLine/selectTimeLine?contactId=${params.contactId}&page=${params.page}&size=${params.size}`;
        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
                return parentCallback({
                    error: '后台报错'
                });
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: '查询出错'
                });
            }
        })
    }


    /**
     * 
     * 获取门店所有订单
     * @param {*} params 
     * @param {*} parentCallback 
     */
    getAssociatorLinkOrder(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;

        var url = `${RetailServer}/dingdang/eBoosDeal/findDeal?page=${parseInt(params.page) + 1}&size=${params.size}&storeId=${params.storeId}`;
        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: '查询出错'
                });
            }
        })
    }

    /**
     * 
     * 顾客交易关联
     * @param {*} params 
     * @param {*} parentCallback 
     */
    setUserOrder(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;

        var url = `${RetailServer}/dingdang/customer?cmd=setDeal`;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: '查询出错'
                });
            }
        })
    }



    getAllTeams(parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url = `${RetailServer}/dingdang/customer?`;
        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        })
    }

    getAllUsers(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url = `${RetailServer}/dingdang/customer?`;
        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        })
    }


    operateUser(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url = '';
        if (params.type == 'add') {
            delete params.employeId;
            url = `${RetailServer}/dingdang/employe/add`;
        } else if (params.type == 'edit') {
            url = `${RetailServer}/dingdang/employe/edit`;
        } else {
            url = `${RetailServer}/dingdang/userRole/updateUserOrderRole`;
        }
        delete params.type;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        })
    }

    operateTask(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url = '';
        if (params.type == 'add') {
            delete params.data.wfTaskId;
            url = `${RetailServer}/dingdang/task/addTask`;
        } else if (params.type == 'delete') {
            url = `${RetailServer}/dingdang/task/deleteTask`;
        } else if (params.type == 'assign') {
            // 标记任务完成
            params.data.taskStatus = params.data.taskStatus == "0" ? "4" : "0";
            delete params.userId;
            url = `${RetailServer}/dingdang/task/modifyTask`;
        } else {
            url = `${RetailServer}/dingdang/task/modifyTask`;
        }
        delete params.type;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        })
    }
    ////////////// 七牛 
    operateQiniu(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url = `${RetailServer}/wxserver/imgUpload/upload?wxUid=${params.wxUid}&wxSid=${params.wxSid}`;
        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                console.log('error')
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.uptoken) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        })
    }
    ////////////// 新增商品
    operateGoods(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url = '';
        switch (params.type) {
            case 'add':
                // VIP客户
                url = `${RetailServer}/wxserver/goods/addGoodsBysSalesclerk`;
                break;
            case 'update':
                url = `${RetailServer}/wxserver/goods/updateGoodsBysSalesclerk`;
        }

        delete params.type;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                console.log('error')
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        })
    }

    /**
     * 创建门店
     * @param {*} params 
     * @param {*} parentCallback 
     */
    createTeam(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url = '';
        if (params.type == 'add') {
            url = `${RetailServer}/dingdang/store/add`;
        } else {
            url = `${RetailServer}/dingdang/store/edit`;
        }
        delete params.type;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        })
    }

    createDevice(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var url = '';
        if (params.type == 'add') {
            delete params.deviceId;
            url = `${RetailServer}/dingdang/device/add`;
        } else {
            url = `${RetailServer}/dingdang/device/update`;
        }
        delete params.type;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        })
    }

    /**
     * 
     * 根据不同条件得到任务列表数据
     * @param {*} params 
     * @param {*} parentCallback 
     */
    getTasks(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        const postParam = {
            data: {
                status: 0,
                page: params.page,
                size: params.size
            }
        };
        let url = `https://dingdian.parllay.cn/wxserver/goods/selectGoodsListBysSalesclerk`;
        switch (params.filter) {
            case 'tasks':
                postParam.data.taskStatus = 0;
                break;
            case 'today':
                postParam.data.taskStatus = 1;
                break;
            case 'week':
                postParam.data.taskStatus = 2;
                break;
            case 'overdue':
                postParam.data.taskStatus = 3;
                break;
            case 'completed':
                postParam.data.taskStatus = 4;
                break;
            default:
                url = `https://dingdian.parllay.cn/wxserver/goods/selectGoodsListBysSalesclerk`;
                postParam.data = {
                    taskGroup: params.filter
                }
                break;

        }
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(postParam)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
                return parentCallback({
                    error: '后台报错'
                });
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        })
    }

    // 设置页的店员和门店的搜索接口
    commonSearch(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        var param, url;
        switch (params.type) {
            case 'user':
                // 查询店员
                param = { page: params.page, size: params.size, storeId: params.storeId, employeName: params.keyword };
                param = queryString.stringify(param);
                url = `${RetailServer}/dingdang/employe?cmd=listEmploye&${param}`;
                break;
            case 'team':
                // 查询门店
                if (params) {
                    param = { page: params.page, size: params.size, storeName: params.keyword }
                    param = queryString.stringify(param);
                    url = `${RetailServer}/dingdang/store?cmd=listStore&${param}`;
                } else {
                    url = `${RetailServer}/dingdang/store?cmd=listStore`;
                }
                break;
            case 'serial':
                url = `${RetailServer}/dingdang/device/selectIpcId?page=${params.page}&size=${params.size}&ipcId=${params.keyword || ''}`;
                break;
            case 'device':
                param = { page: params.page, size: params.size, storeId: params.storeId, deviceName: params.keyword };
                param = queryString.stringify(param);
                url = `${RetailServer}/dingdang/device/selectDevice?${param}`;
                break;
            case 'size':
                url = `${RetailServer}/dingdang/code?cmd=listSizeName`;
                break;
            case 'mobile':
                url = `${RetailServer}/dingdang/employe/selectMobile?mobile=${params.keyword}`;
                break;
            case 'brand':
                param = { page: params.page, size: params.size, brandName: params.keyword };
                param = queryString.stringify(param);
                url = `${RetailServer}/dingdang/brand/selectBrand?${param}`;
                break;
            case 'role':
                url = `${RetailServer}/dingdang/userRole/selectRoleIds?userId=${params.userId}`;
                break;
        }

        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })
    }

    // 客户、订单、任务组、店员等搜索功能
    tasksCommonSearch(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        let url, param = {
            page: params.page,
            size: params.size
        }
        switch (params.type) {
            case 'contact':
                url = `${RetailServer}/wxserver/activity/getActivityGoods?`;
                param.storeId = params.storeId;
                param.customerName = params.keyword;
                break;
            case 'deal':
                url = `${RetailServer}/dingdang/dealshow/find?`;
                param.billNo = params.keyword;
                break;
            case 'queue':
                url = `${RetailServer}/dingdang/queue/find?`;
                param.queueName = params.keyword;
                break;

        }

        url = `${url}${queryString.stringify(param)}`;

        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })

    }

    goodsGroupOperation(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        let url;
        if (params.type == 'add') {
            url = `${RetailServer}/wxserver/goods/addGoodsBysSalesclerk`;
        }
        delete params.type;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
                return parentCallback({
                    error: '后台查询报错'
                });
            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })

    }

    taskGroupOperation(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        let url;
        if (params.type == 'add') {
            url = `${RetailServer}/dingdang/taskGroup/addTaskGroup`;
        } else {
            url = `${RetailServer}/dingdang/taskGroup/getTaskGroupInfo`;
        }
        delete params.type;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
                return parentCallback({
                    error: '后台查询报错'
                });
            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })

    }

    /**
     * 
     * 获取客流量数据
     * @param {*} params 
     * @param {*} parentCallback 
     */
    getTrafficData(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        let url;
        switch (params) {
            case 'week':
                url = `${RetailServer}/dingdang/report/getArrivalCountByWeek`;
                break;
            case 'month':
                url = `${RetailServer}/dingdang/report/getArrivalCountByMonth`;
                break;
            default:
                url = `${RetailServer}/dingdang/report/getArrivalCountByHour`;
        }

        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })
    }

    /**
     * 
     * 获取普通和VIP占比数据
     * @param {*} params 
     * @param {*} parentCallback 
     */
    getOccupyData(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        let url
        switch (params.type) {
            case 'traffic':
                url = `${RetailServer}/dingdang/report/getArrivalCountByDay?storeId=${params.storeId}&startTime=${params.startTime}&endTime=${params.endTime}`;
                break;
            case 'total':
                url = `${RetailServer}/dingdang/report/isvipCompare?storeId=${params.storeId}`;
                break;
            case 'gender':
                url = `${RetailServer}/dingdang/report/genderCompare?storeId=${params.storeId}`;
                break;
            case 'age':
                url = `${RetailServer}/dingdang/report/ageGroupCompare?storeId=${params.storeId}`;
                break;
            case 'sequential':
                // 环比
                url = `${RetailServer}/dingdang/report/getArrivalCountByWeekLink?storeId=${params.storeId}&startTime=${params.startTime}&endTime=${params.endTime}`;
                break;
            case 'vipIncrease':
                url = `${RetailServer}/dingdang/report/getVipDailyChangeByDay?storeId=${params.storeId}&startTime=${params.startTime}&endTime=${params.endTime}`;
                break;
            case 'amountAndCount':
                url = `${RetailServer}/dingdang/report/getArrivalCountAndAmount?storeId=${params.storeId}&startTime=${params.startTime}&endTime=${params.endTime}`;
                break;
            case 'vipAmountAndCount':
                url = `${RetailServer}/dingdang/report/getVipCountAndAmount?storeId=${params.storeId}&startTime=${params.startTime}&endTime=${params.endTime}`;
                break;
        }

        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })
    }

    updateUserProfile(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        let url = `${RetailServer}/dingdang/user/modifyUser`;
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(params)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback({ success: true, result: result });
        })
    }


    /**
     * 
     * 获取销售任务中的各个报表接口
     * @param {*} type 
     * @param {*} parentCallback 
     */
    getSalesData(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        let url
        switch (params.type) {
            case 'amount':
                // 销售额
                url = `${RetailServer}/dingdang/report/getAmount`;
                break;
            case 'rate':
                // 连带率
                url = `${RetailServer}/dingdang/report/getJointRate`;
                break;
            case 'price':
                // 客单价
                url = `${RetailServer}/dingdang/report/getGuestPrice`;
                break;
            case 'complete':
                url = `${RetailServer}/dingdang/report/getRatio`;
                break;
            case 'count':
                // 客单价
                url = `${RetailServer}/dingdang/report/getDealCount`;
                break;
            case 'ordinaryIncrease':
                url = `${RetailServer}/dingdang/report/getArrivalCountByWeekLink`;
                break;
            case 'amountAndCount':
                url = `${RetailServer}/dingdang/report/getArrivalCountAndAmount`;
                break;
            default:
                // 获取前5个订单消费金额最多的顾客
                url = `${RetailServer}/dingdang/report/getDealTopFive`;
                break;
        }
        if (params.type != 'top') {
            url += `?storeId=${params.storeId}&startTime=${params.startTime}&endTime=${params.endTime}`;
        } else {
            url += `?storeId=${params.storeId}`;
        }
        request(url, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })
    }

}

exports.RetailService = RetailService;